﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;
using VOL.Smartmanufacturing.Infrastructure;
using VOL.Smartmanufacturing.IServices.ProcessesProcedure;

namespace VOL.Smartmanufacturing.Services.ProcessesProcedure
{
    /// <summary>
    /// 服务接口实现层--工艺路线tsh
    /// </summary>
    public  class CraftRouteServices: ICraftRouteServices
    {
        private readonly IBaseRepository<CraftRoute> _craftRouteRepository;

        public IBaseRepository<CraftManagement> _craftManagementRepository { get; }

        public CraftRouteServices(IBaseRepository<CraftManagement> craftManagementRepository , IBaseRepository<CraftRoute> craftRouteRepository)
        {
            this._craftManagementRepository = craftManagementRepository;
            this._craftRouteRepository = craftRouteRepository;
        }

        /// <summary>
        /// 新增工艺路线
        /// </summary>
        /// <param name="craftRoute">实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddCraftRoute(CraftRoute craftRoute)
        {
            try
            {
                int i = await _craftRouteRepository.AddAsync(craftRoute);
                return i;
            }
            catch (Exception)
            {
                throw;
            }
           
        }

        /// <summary>
        /// 修改工艺路线
        /// </summary>
        /// <param name="craftRoute">实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateCraftRoute(CraftRoute craftRoute)
        {
            try
            {
                int i = await _craftRouteRepository.UpdateAsync(craftRoute);
                return i;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除工艺路线
        /// </summary>
        /// <param name="id">主键编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeleteCraftRoute(int id)
        {
            try
            {
                var craftRoute = await _craftRouteRepository.FindAsync(id);
                int i = await _craftRouteRepository.Remove(craftRoute);
                return i;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有工艺路线信息
        /// </summary>
        /// <returns>集合</returns>
        public ApiPaging<CraftRoute> GetCraftRouteList(int pageIndex, int pageSize)
        {
            try
            {
                ApiPaging<CraftRoute> apiPaging = new ApiPaging<CraftRoute>();
                var craftRouteList = _craftRouteRepository.GetAll();
                apiPaging.TotalCount = craftRouteList.Count();
                apiPaging.TotalPage = (int)Math.Ceiling(craftRouteList.Count() * 1.0 / pageSize);
                apiPaging.Items = craftRouteList.OrderByDescending(a => a.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                return apiPaging;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取所有工序管理信息
        /// </summary>
        /// <returns>集合</returns>
        public IQueryable<CraftManagement> GetCraftManagementList()
        {
            try
            {
                var list = _craftManagementRepository.GetAll();
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
